home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / emacs.lha / emacs-19.16 / lisp / uncompress.el < prev    next >
Lisp/Scheme  |  1993-03-24  |  3KB  |  83 lines

  1. ;;; uncompress.el --- auto-decompression hook for visiting .Z files
  2.  
  3. ;; Copyright (C) 1992 Free Software Foundation, Inc.
  4.  
  5. ;; Maintainer: FSF
  6.  
  7. ;; This file is part of GNU Emacs.
  8.  
  9. ;; GNU Emacs is free software; you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation; either version 2, or (at your option)
  12. ;; any later version.
  13.  
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. ;; GNU General Public License for more details.
  18.  
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  21. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  
  23. ;;; Commentary:
  24.  
  25. ;; This package can be used to arrange for automatic uncompress of
  26. ;; files packed with the UNIX compress(1) utility when they are visited.
  27. ;; All that's necessary is to load it.  This can conveniently be done from
  28. ;; your .emacs file.
  29.  
  30. ;;; Code:
  31.  
  32. ;; When we are about to make a backup file,
  33. ;; uncompress the file we visited
  34. ;; so that making the backup can work properly.
  35. ;; This is used as a write-file-hook.
  36.  
  37. (defun uncompress-backup-file ()
  38.   (and buffer-file-name make-backup-files (not buffer-backed-up)
  39.        (not (file-exists-p buffer-file-name))
  40.        (call-process "uncompress" nil nil nil buffer-file-name))
  41.   nil)
  42.  
  43. (or (assoc "\\.Z$" auto-mode-alist)
  44.     (setq auto-mode-alist
  45.       (cons '("\\.Z$" . uncompress-while-visiting) auto-mode-alist)))
  46.  
  47. (defun uncompress-while-visiting ()
  48.   "Temporary \"major mode\" used for .Z files, to uncompress the contents.
  49. It then selects a major mode from the uncompressed file name and contents."
  50.   (if (and (not (null buffer-file-name))
  51.        (string-match "\\.Z$" buffer-file-name))
  52.       (set-visited-file-name
  53.        (substring buffer-file-name 0 (match-beginning 0))))
  54.   (message "Uncompressing...")
  55.   (let ((buffer-read-only nil))
  56.     (shell-command-on-region (point-min) (point-max) "uncompress" t))
  57.   (message "Uncompressing...done")
  58.   (set-buffer-modified-p nil)
  59.   (make-local-variable 'write-file-hooks)
  60.   (or (memq 'uncompress-backup-file write-file-hooks)
  61.       (setq write-file-hooks (cons 'uncompress-backup-file write-file-hooks)))
  62.   (normal-mode))
  63.  
  64. (or (memq 'find-compressed-version find-file-not-found-hooks)
  65.     (setq find-file-not-found-hooks
  66.       (cons 'find-compressed-version find-file-not-found-hooks)))
  67.  
  68. (defun find-compressed-version ()
  69.   "Hook to read and uncompress the compressed version of a file."
  70.   ;; Just pretend we had visited the compressed file,
  71.   ;; and uncompress-while-visiting will do the rest.
  72.   (if (file-exists-p (concat buffer-file-name ".Z"))
  73.       (progn
  74.     (setq buffer-file-name (concat buffer-file-name ".Z"))
  75.     (insert-file-contents buffer-file-name t)
  76.     (goto-char (point-min))
  77.     (setq error nil)
  78.     t)))
  79.  
  80. (provide 'uncompress)
  81.  
  82. ;;; uncompress.el ends here
  83.